package sedev.struts.portlet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONArray;
import org.json.JSONObject;

import sedev.shopping.business.HinhThucDichVuService;
import sedev.shopping.business.LoaiSanPhamService;
import sedev.shopping.business.NhomSanPhamService;
import sedev.shopping.entities.ListHinhThucDichVu;
import sedev.shopping.entities.ListLoaiSanPham;
import sedev.shopping.entities.ListNhomSanPham;
import sedev.shopping.entities.LoaiSanPham;
import sedev.shopping.util.ConfigUtil;
import sedev.shopping.util.Paging;
import sedev.shopping.util.SedevUtil;
import sedev.shopping.util.SedevUtil.ModeDisplay;

import com.liferay.portal.util.PortalUtil;
import com.opensymphony.xwork2.ActionSupport;

public class ProductTypeAction extends ActionSupport implements
		ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private long ownerId;
	
	private int preExecute() {
		if(SedevUtil.isOwner(this.request) == false){
			return -1;
		}			
		return 0;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		ownerId = PortalUtil.getUserId(this.request);
	}

	@Override
	public String execute() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		String actionResult = "";
		Paging paging = new Paging();
		if(this.request.getParameter("shopping_page_manager") != null){
			paging.setCurrent(Integer.valueOf(this.request.getParameter("shopping_page_manager")));
		}
		paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.pagesize","5")));
		paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.numdisplay","10")));
		ListLoaiSanPham listdata = LoaiSanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.All);
		this.request.setAttribute("listloaisanpham", listdata);
		this.request.setAttribute("paging", paging);
		actionResult = ActionSupport.SUCCESS;
		return actionResult;
	}

	public String post() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		ListNhomSanPham listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.Display);
		ListHinhThucDichVu listhinhthucdichvu = HinhThucDichVuService.getListDataByAll(SedevUtil.Order.ASC);		
		this.request.setAttribute("nhomsanpham", this.jsonEncodeNhomSanPham(listnhomsanpham));
		this.request.setAttribute("listhinhthucdichvu", listhinhthucdichvu);
		return ActionSupport.SUCCESS;
	}

	public String edit() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		LoaiSanPham data = new LoaiSanPham();
		try {
			Long id = Long.valueOf((String) this.request.getParameter("typeid"));
			data = LoaiSanPhamService.getData(id);
			if (data != null) {
				this.request.setAttribute("loaisanpham", data);
				ListNhomSanPham listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.Display);
				ListHinhThucDichVu listhinhthucdichvu = HinhThucDichVuService.getListDataByAll(SedevUtil.Order.ASC);
				this.request.setAttribute("nhomsanpham", this.jsonEncodeNhomSanPham(listnhomsanpham));
				this.request.setAttribute("listhinhthucdichvu", listhinhthucdichvu);
				return ActionSupport.SUCCESS;
			} else{
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return index page
		Paging paging = new Paging();
		paging.setCurrent(1);		
		paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.pagesize","5")));
		paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.numdisplay","10")));
		ListLoaiSanPham listdata = LoaiSanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.All);
		this.request.setAttribute("listloaisanpham", listdata);
		this.request.setAttribute("paging", paging);
		return ActionSupport.ERROR;
	}

	public String add() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		LoaiSanPham data = new LoaiSanPham();
		try {
			if (this.initData(data)) {
				if (LoaiSanPhamService.addData(data)) {
					this.request.setAttribute("errorCode", 1);
					this.request.setAttribute("loaisanpham", data);					
					ListNhomSanPham listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.Display);
					ListHinhThucDichVu listhinhthucdichvu = HinhThucDichVuService.getListDataByAll(SedevUtil.Order.ASC);
					this.request.setAttribute("nhomsanpham", this.jsonEncodeNhomSanPham(listnhomsanpham));
					this.request.setAttribute("listhinhthucdichvu", listhinhthucdichvu);
					return ActionSupport.SUCCESS;
				}
			}else{
				this.request.setAttribute("errorCode", -1);
			}			
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return post page
		ListNhomSanPham listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.Display);
		ListHinhThucDichVu listhinhthucdichvu = HinhThucDichVuService.getListDataByAll(SedevUtil.Order.ASC);
		this.request.setAttribute("listnhomsanpham", listnhomsanpham);
		this.request.setAttribute("listhinhthucdichvu", listhinhthucdichvu);
		return ActionSupport.ERROR;
	}

	public String update() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		LoaiSanPham data = new LoaiSanPham();
		try {
			Long id = Long.valueOf(this.request.getParameter("id"));
			data = LoaiSanPhamService.getData(id);
			if (data != null) {
				if (this.initData(data)) {
					if (LoaiSanPhamService.updateData(data)) {
						this.request.setAttribute("errorCode", 2);
					} else {
						this.request.setAttribute("errorCode", -1);
					}
				} else {
					this.request.setAttribute("errorCode", -1);
				}
				this.request.setAttribute("loaisanpham", data);
				ListNhomSanPham listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.Display);
				ListHinhThucDichVu listhinhthucdichvu = HinhThucDichVuService.getListDataByAll(SedevUtil.Order.ASC);
				this.request.setAttribute("nhomsanpham", this.jsonEncodeNhomSanPham(listnhomsanpham));
				this.request.setAttribute("listhinhthucdichvu", listhinhthucdichvu);
				return ActionSupport.SUCCESS;
			}else{
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return index page
		Paging paging = new Paging();
		if(this.request.getParameter("shopping_page_manager") != null){
			paging.setCurrent(Integer.valueOf(this.request.getParameter("shopping_page_manager")));
		}	
		paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.pagesize","5")));
		paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.numdisplay","10")));
		ListLoaiSanPham listdata = LoaiSanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.All);
		this.request.setAttribute("listloaisanpham", listdata);
		this.request.setAttribute("paging", paging);
		return ActionSupport.ERROR;
	}

	public String remove() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		long id = Long.valueOf(this.request.getParameter("id"));
		if(LoaiSanPhamService.removeData(id)){
			this.request.setAttribute("errorCode", 1);			
		}else{
			this.request.setAttribute("errorCode", -1);
		}
		Paging paging = new Paging();
		if(this.request.getParameter("shopping_page_manager") != null){
			paging.setCurrent(Integer.valueOf(this.request.getParameter("shopping_page_manager")));
		}
				
		paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.pagesize","5")));
		paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.numdisplay","10")));
		ListLoaiSanPham listdata = LoaiSanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.All);
		this.request.setAttribute("listloaisanpham", listdata);
		this.request.setAttribute("paging", paging);
		return ActionSupport.SUCCESS;
	}

	private boolean initData(LoaiSanPham data) {
		try {			
			data.setTen(this.request.getParameter("title"));
		} catch (Exception ex) {
			data.setTen("");
		}
		try {
			data.setNhomSanPhamId(Long.valueOf(this.request
					.getParameter("groupproduct")));
		} catch (Exception ex) {
			data.setNhomSanPhamId(Long.valueOf(0));
		}
		try {
			data.setHinhThucDichVuId(Integer.valueOf(this.request
					.getParameter("businesstype")));
		} catch (Exception ex) {
			data.setHinhThucDichVuId(0);
		}
		try {			
			data.setThuocTinh(this.request.getParameter("attribute"));
		} catch (Exception ex) {
			data.setThuocTinh("");
		}		
		try {
			Integer isHienThi = Integer.valueOf(this.request.getParameter("published"));
			if (isHienThi > 0) {
				data.setHienThi(true);
			} else {
				data.setHienThi(false);
			}
		} catch (Exception ex) {
			data.setHienThi(false);
		}		
		try {
			if(data.getNgayDang()== null){
				data.setNgayDang(SedevUtil.getTime());
			}
		} catch (Exception ex) {
			data.setNgayDang(Long.valueOf(0));
		}
		if(data.getId() == null){
			data.setUserId(this.ownerId);
		}			
		return true;
	}
	
	private String jsonEncodeNhomSanPham(ListNhomSanPham listdata){
    	JSONObject jObj = new JSONObject();
    	JSONArray jArray = new JSONArray();
    	for(int index=0; index<listdata.size(); ++index){
    		JSONObject jObjItem = new JSONObject();
    		jObjItem.put("id", listdata.get(index).getId());
    		jObjItem.put("parentId", listdata.get(index).getNhomSanPhamId());
    		jObjItem.put("name", listdata.get(index).getTen());
    		jObjItem.put("data", listdata.get(index).getThuocTinh());
    		jArray.put(jObjItem);
    	}
    	jObj.put("data", jArray);
    	return jObj.toString();
    }
	
}
